package com.system.seckillgoods.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.common.bean.Address;
import com.system.common.bean.Goods;
import com.system.common.bean.SeckillGoods;
import com.system.common.exception.ServiceException;
import com.system.common.service.IGoodsService;
import com.system.common.service.ISeckillActivityService;
import com.system.common.service.ISeckillGoodsService;
import com.system.seckillgoods.mapper.SeckillGoodsMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.Date;
import java.util.List;

@DubboService
public class SeckillGoodsServiceImpl extends ServiceImpl<SeckillGoodsMapper, SeckillGoods> implements ISeckillGoodsService {


    @DubboReference
    private IGoodsService goodsService;
    @Override
    public SeckillGoods getSeckillGoodsById(Integer id) {
        return super.getById(id);
    }

    @Override
    public SeckillGoods getSeckillGoodsByGoodsId(Integer goodsId) {
        LambdaQueryWrapper<SeckillGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillGoods::getGoodsid, goodsId);
        return super.getOne(queryWrapper);
    }

    @Override
    public void addSeckillGoods(SeckillGoods seckillGoods) {
        super.save(seckillGoods);
    }

    @Override
    public void updateSeckillGoods(SeckillGoods seckillGoods) {
        super.updateById(seckillGoods);
    }

    @Override
    public void deleteSeckillGoods(Integer id) {
        SeckillGoods seckillGoods = super.getById(id);
        if (seckillGoods == null) {
            throw new ServiceException("秒杀商品不存在");
        }
        if (seckillGoods.getIsdel() == 1) {
            throw new ServiceException("秒杀商品已删除");
        }
        super.removeById(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        SeckillGoods seckillGoods = super.getById(id);
        if (seckillGoods == null) {
            throw new ServiceException("秒杀商品不存在");
        }
        if (seckillGoods.getIsdel() == 1) {
            throw new ServiceException("秒杀商品已删除");
        }
        seckillGoods.setStatus(status);
        super.updateById(seckillGoods);
    }

    @Override
    public List<SeckillGoods> getSeckillGoodsList() {
        return super.list();
    }

    @Override
    public Page<SeckillGoods> getPage(SeckillGoods seckillGoods, Page<SeckillGoods> page) {
        LambdaQueryWrapper<SeckillGoods> queryWrapper = new LambdaQueryWrapper<>();
        if (seckillGoods.getActivityid() != null) {
            queryWrapper.eq(SeckillGoods::getActivityid, seckillGoods.getActivityid());
        }
        if (seckillGoods.getGoodsid() != null) {
            queryWrapper.eq(SeckillGoods::getGoodsid, seckillGoods.getGoodsid());
        }

        Page<SeckillGoods> resultPage = baseMapper.selectPage(page, queryWrapper);
        List<SeckillGoods> records = resultPage.getRecords();
        for (SeckillGoods good : records) {
            if (good.getGoodsid() != null){
                Goods g = goodsService.getGoodsById(good.getGoodsid());
                good.setGoods(g);
            }
        }
        Long total = this.count(queryWrapper);
        resultPage.setTotal(total);
        return resultPage;
    }


    @Override
    public boolean reduceStock(Integer id, Integer count) {
        SeckillGoods seckillGoods = super.getById(id);
        if (seckillGoods == null || seckillGoods.getIsdel() == 1) {
            throw new ServiceException("秒杀商品不存在或已删除");
        }
        if (seckillGoods.getStock() < count) {
            return false; // 库存不足
        }
        seckillGoods.setStock(seckillGoods.getStock() - count);
        return super.updateById(seckillGoods);
    }
    @Override
    public List<SeckillGoods> getByActivityId(Integer activityId) {
        LambdaQueryWrapper<SeckillGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillGoods::getActivityid, activityId)
                .eq(SeckillGoods::getIsdel, 0);
        return super.list(queryWrapper);
    }

}
